#!/usr/bin/python3
import curses
import time
import random
import copy
def generate_sudoku_board():
    board = [[0] * 9 for _ in range(9)]
    def filling_board(row, col):
        if row == 9:
            return True
        next_row = row if col < 8 else row + 1
        next_col = (col + 1) % 9
        box_row = row // 3
        box_col = col // 3
        numbers = random.sample(range(1, 10), 9)
        for num in numbers:
            if num not in board[row] and all(board[i][col] != num for i in range(9)) and all(num != board[i][j] for i in range(box_row*3, box_row*3+3) for j in range(box_col*3, box_col*3+3)):
                board[row][col] = num
                if filling_board(next_row, next_col):
                    return True
                board[row][col] = 0
        return False
    filling_board(0, 0)
    return board
def create_board(level):
        board = generate_sudoku_board()
        board1 =  copy.deepcopy(board)
        for i in range(81):
            row = i // 9
            col = i % 9
            if random.randint(0, 9) > int(level):
                board1[row][col] = 0
        return (board,board1)

stdscr = curses.initscr()
curses.noecho()
curses.cbreak()
stdscr.keypad(True)
curses.start_color()
curses.use_default_colors()
color_pairs = 1
for c in (curses.COLOR_BLUE, curses.COLOR_CYAN, curses.COLOR_GREEN,
          curses.COLOR_MAGENTA,curses.COLOR_RED, curses.COLOR_YELLOW,
          curses.COLOR_BLACK):
    curses.init_pair(color_pairs, c, -1)
    color_pairs += 1

n=create_board(random.randint(4,8))
n1=n[0]
n2=n[1]
m=0
dd=0
for qw in n2:
    for wq in qw:
        if wq == 0:
            stdscr.addstr("■ ",curses.color_pair(6))
        else:
            stdscr.addstr(str(wq)+" ")
        
    m+=1
    stdscr.addstr(m,0,"")
stdscr.refresh()
stdscr.move(0,0)
bv=[0,0]
num=(ord("1"),ord("2"),ord("3"),ord("4"),ord("5"),ord("6"),ord("7"),ord("8"),ord("9"))
wha=("ȱ","Ȳ","ȳ","ȴ","ȵ","ȶ","ȷ","ȸ","ȹ")
stdscr.refresh()
while True:
    key = stdscr.getch()
    if key == curses.KEY_DOWN and bv[0] <8:
        bv=[bv[0]+1,bv[1]]
        stdscr.move(bv[0],bv[1])
        stdscr.refresh()
    if key == curses.KEY_UP and bv[0] >0:
        bv=[bv[0]-1,bv[1]]
        stdscr.move(bv[0],bv[1])
        stdscr.refresh()
    if key == curses.KEY_RIGHT and bv[1] <16:
        bv=[bv[0],bv[1]+2]
        stdscr.move(bv[0],bv[1])
        stdscr.refresh()
    if key == curses.KEY_LEFT and bv[1] >0:
        bv=[bv[0],bv[1]-2]
        stdscr.move(bv[0],bv[1])
        stdscr.refresh()
    if key == ord("Q"):
        curses.nocbreak()
        stdscr.keypad(False)
        curses.echo()
        curses.endwin()
        quit()
    if key in num:
        if n2[bv[0]][int(bv[1]/2)] == 0:
            stdscr.addstr(chr(key),curses.color_pair(2))
            stdscr.move(bv[0],bv[1])
    if key == ord("c"):
        if chr(stdscr.inch()) in wha:
            if wha.index(chr(stdscr.inch()))+1 == n1[bv[0]][int(bv[1]/2)]:
                n2[bv[0]][int(bv[1]/2)]=wha.index(chr(stdscr.inch()))+1
                stdscr.addstr(str(wha.index(chr(stdscr.inch()))+1),curses.color_pair(3))

                stdscr.move(bv[0],bv[1])
            else:
                stdscr.addstr(str(wha.index(chr(stdscr.inch()))+1),curses.color_pair(5))
                stdscr.move(bv[0],bv[1])
                dd+=1
    if n2==n1:
        time.sleep(2)
        stdscr.addstr("你赢了",curses.color_pair(3))
        stdscr.refresh()
        time.sleep(5)
        curses.nocbreak()
        stdscr.keypad(False)
        curses.echo()
        curses.endwin()
        quit()
    if dd>= 5:
        time.sleep(2)
        stdscr.addstr("你输了",curses.color_pair(5))
        stdscr.refresh()
        time.sleep(5)
        curses.nocbreak()
        stdscr.keypad(False)
        curses.echo()
        curses.endwin()
        quit()

    stdscr.refresh()

